package com.xjj.service.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.xjj.entity.Menu;
import com.xjj.entity.ResponseEntity;
import com.xjj.service.service.MenuService;
import com.xjj.util.utils.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author : 王泰元
 * @date : 2022/10/20 11:31
 */
@Api(tags = "后台管理菜单相关接口")
@RestController
@RequestMapping("/auth/admin/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;

    @ApiOperation("查询所有菜单")
    @GetMapping("/getAll")
    public ResponseEntity<List<Menu>> getAll() {

        List<Menu> menus = menuService.getAll();

        return ResponseEntity.SUCCESS("ok", menus);
    }

    @ApiOperation("根据角色id查询菜单")
    @GetMapping("/getMenus")
    public ResponseEntity<List<Menu>> getMenus(HttpServletRequest request) throws JsonProcessingException {

        String token = request.getHeader("token");
        Integer roleId = JwtUtil.getManager(token).getRoleId();

        List<Menu> menus = menuService.getMenusByRoleId(roleId);

        return ResponseEntity.SUCCESS("ok", menus);
    }
}
